<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form class="layui-form" id="admin">

    <table class="layui-table" lay-even="" lay-skin="row" lay-size="sm">
        <colgroup>
            <col width="100">
            <col width="150">
        </colgroup>
        <thead>
        <tr>
            <th>类型名称</th>
            <th>默认价格</th>
        </tr>
        </thead>
        <tbody>

            {volist name="child_info" id="vo"}
            <tr>
                <td>
                    <input type="hidden" class="child_id" value="{$vo.price_type_id}">
                    <input type="text" class="price_name" value="{$vo.type_name}" style="width:100%;height:30px;">
                </td>
                <td>
                    <input type="text" class="price_value" value="{$vo.value}" style="width:90%;height:30px;">
                    <span class="layui-layer-setwin">
                        <a class="input-close layui-layer-ico layui-layer-close layui-layer-close1" href="javascript:;">
                        </a>
                    </span>
                </td>
            </tr>
            {/volist}

            <tr>
                <td style="text-align: center">
                    <button class="layui-btn layui-btn-primary layui-btn-sm" id="add_btn">
                        <i class="layui-icon">&#xe654;</i>
                    </button>
                </td>
                <td></td>
            </tr>
        </tbody>
    </table>

    {notempty name="$price_type_id"}
    <input type="hidden" name="price_type_id" id="price_type_id" value="{$price_type_id}">
    {/notempty}

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
            <button id="reset1" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script src="__PUBLIC__/layui/layui.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>
<script>

    $("#add_btn").click(function(){
        $(this).parent().parent().before('<tr><td><input type="hidden" class="child_id" value="0"><input type="text" class="price_name" value="" style="width:100%;height:30px;"></td><td><input type="text" class="price_value" value="" style="width:90%;height:30px;"><span class="layui-layer-setwin"><a class="input-close layui-layer-ico layui-layer-close layui-layer-close1" href="javascript:;"></a></span></td></tr>');
        return false;
    });

    $("#reset1").click(function(){
        $(".input-close").parent().parent().parent().remove();
        return false;
    });

    $(document).on('click', '.input-close', function() {
        $(this).parent().parent().parent().remove();
        return false;
    });

    layui.use(['layer', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form;
        $(window).on('load', function() {
            form.on('submit(admin)', function(data) {
                var price_type_id = $("#price_type_id").attr("value");
                var post_arr = [];
                var flag = 0;
                var is_complement_flag = 0;
                $(".price_name").each(function(){
                    var name = $(this).val();
                    var child_id = $(this).parent().find(".child_id").val();
                    var value = $(this).parent().parent().find(".price_value").val();
                    if(name=='' && value!==''){
                        is_complement_flag = 1;
                    }else{
                        post_arr.push([child_id,name,value]);
                        flag++;
                    }
                });

                if(is_complement_flag==1){
                    layer.msg("请补全类型名称");
                    return false;
                }

                var post_arr_json = JSON.stringify(post_arr);

                if(flag > 0){
                    $.ajax({
                        url:"{:url('admin/tripprice/addchild')}",
                        data:{'post_arr':post_arr_json,'price_type_id':price_type_id},
                        type:'post',
                        async: false,
                        success:function(res) {
                            console.log(res);
                            layer.msg(res.msg);
                            if(res.code == 1) {
                                setTimeout(function(){
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                },1500);
                            }
                        }
                    })
                }else{
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }
                return false;
            });
        });
    });
</script>
</body>
</html>
